/**
 * Created by Freeman on 2016/7/7.
 */
var elastic = require('./elasticsearch'),
    Blog = require('../models/article.model');
elastic.indexExists().then(function (exists) {
    if (exists) {
        return elastic.deleteIndex();
    }
}).then(function () {
    return elastic.initIndex().then(elastic.initMapping).then(function () {
        var condition = {status:{$gt:0}};
        Blog.find(condition)
            .populate({
                path: 'author_id',
                select: 'nickname avatar'
            })
            .select('title content visit_count comment_count like_count publish_time author_id')
            .exec().then(function (list) {
                list.map(function (item) {
                    return elastic.addDocument(item);
                });

        });
    });
});